#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "my_complex.h"
#include <math.h>

double* complex_plus(double a_re,double a_im,double b_re,double b_im){
    double* result_D= new double[2];
     result_D[0]=a_re+b_re;
     result_D[1]= a_im+b_im;
return( result_D);
}
double* complex_mnozj(double a_re,double a_im,double b_re,double b_im){
    double* result_C= new double[2];
     result_C[0]=(a_re*b_re)-(a_im*b_im);
     result_C[1]= a_re*b_im+a_im*b_re;
return(result_C);
}
double* complex_minus (double a_re,double a_im,double b_re,double b_im){
    double* result_D= new double[2];
     result_D[0]=a_re-b_re;
     result_D[1]= a_im-b_im;
return( result_D);
}
double* complex_delen(double a_re,double a_im,double b_re,double b_im){//funct_for
    double* result_C= new double[2];
     result_C[0]=((a_re*b_re)+(a_im*b_im))/(b_re*b_re+b_im*b_im);//result-real
     result_C[1]= (b_re*a_im-b_im*a_re)/(b_re*b_re+b_im*b_im);//result_im
return(result_C);
}
double* complex_modul(double a_re,double a_im){//funct_for
    double* result_C= new double[2];
     result_C[0]=sqrt(a_re*a_re+a_im*a_im);//result-real
     result_C[1]= 0;//result_im
return(result_C);
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->result_re->setEnabled(false);
    ui->result_im->setEnabled(false);
    }


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_funct_plus_clicked()
{
    if(ui->my_lib_radio->isChecked())
      {
        double* a=complex_plus(ui->x1_re->text().toDouble(),ui->x1_im->text().toDouble(),ui->x2_re->text().toDouble(),ui->x2_im->text().toDouble());
        QString odin;
        ui->result_re->setText(odin.setNum(a[0]));
        ui->result_im->setText(odin.setNum(a[1]));
      }
    else {
       /* //complex z = complex(x,y);
        double x=ui->x1_re->text().toDouble();
        double y=ui->x1_im->text().toDouble();
       my_complex a = my_complex(x,y);
       my_complex z1 = my_complex(ui->x2_re->text().toDouble(),ui->x2_im->text().toDouble());
       my_complex res=z+z1;
        ui->result_re->setText(real(res));
       ui->result_im->setText(imag(res));
*/
    }
}

void MainWindow::on_funct_mnozj_clicked()
{
    if(ui->my_lib_radio->isChecked())
      {
    double* a=complex_mnozj(ui->x1_re->text().toDouble(),ui->x1_im->text().toDouble(),ui->x2_re->text().toDouble(),ui->x2_im->text().toDouble());
    QString odin;
    ui->result_re->setText(odin.setNum(a[0]));
    ui->result_im->setText(odin.setNum(a[1]));
    }
}




void MainWindow::on_funct_delen_clicked()
{
    if(ui->my_lib_radio->isChecked())
      {
    double* a=complex_delen(ui->x1_re->text().toDouble(),ui->x1_im->text().toDouble(),ui->x2_re->text().toDouble(),ui->x2_im->text().toDouble());
    QString odin;
    ui->result_re->setText(odin.setNum(a[0]));
    ui->result_im->setText(odin.setNum(a[1]));
    }
   }


void MainWindow::on_funct_minus_clicked()
{
    if(ui->my_lib_radio->isChecked())
      {
    double* a=complex_minus(ui->x1_re->text().toDouble(),ui->x1_im->text().toDouble(),ui->x2_re->text().toDouble(),ui->x2_im->text().toDouble());
    QString odin;
    ui->result_re->setText(odin.setNum(a[0]));
    ui->result_im->setText(odin.setNum(a[1]));
    }
}

void MainWindow::on_funct_module_clicked()
{
    if(ui->my_lib_radio->isChecked())
      {
    double* a=complex_modul(ui->x1_re->text().toDouble(),ui->x1_im->text().toDouble());
    QString odin;
    ui->result_re->setText(odin.setNum(a[0]));
    ui->result_im->setText(odin.setNum(a[1]));
    }
}
